package com.shujia;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Demo6MapReduceWriteHBase {
    public static class MyMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            // 将一条数据直接作为key传给reduce
            context.write(new Text(value.toString()), NullWritable.get());
        }
    }

    public static class WriteHBaseReducer extends TableReducer<Text, NullWritable, NullWritable> {
        @Override
        protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
            // 直接取出一条数据
            String students = key.toString();
            String[] splits = students.split(",");
            String id = splits[0];
            String name = splits[1];
            String age = splits[2];
            String gender = splits[3];
            String clazz = splits[4];
            Put put = new Put(id.getBytes());
            put.addColumn("info".getBytes(), "name".getBytes(), name.getBytes());
            put.addColumn("info".getBytes(), "age".getBytes(), age.getBytes());
            put.addColumn("info".getBytes(), "gender".getBytes(), gender.getBytes());
            put.addColumn("info".getBytes(), "clazz".getBytes(), clazz.getBytes());

            // create 'students_re','info'
            context.write(NullWritable.get(), put);
        }
    }


    public static void main(String[] args) throws InterruptedException, IOException, ClassNotFoundException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");

        // 创建Job实例
        Job job = Job.getInstance(conf);
        job.setJarByClass(Demo6MapReduceWriteHBase.class);
        job.setJobName("Demo6MapReduceWriteHBase");

        // 配置Mapper
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        // 配置Reducer
        TableMapReduceUtil.initTableReducerJob(
                "students_re",
                WriteHBaseReducer.class,
                job
        );

        // 指定输入输出目录
        FileInputFormat.addInputPath(job, new Path("/data/students/"));


        job.waitForCompletion(true);

        /**
         * create 'students_re','info'
         *
         * hadoop jar HBaseJavaAPI10-1.0-jar-with-dependencies.jar com.shujia.Demo6MapReduceWriteHBase
         */
    }
}
